package com.shengqin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.shengqin.config.handlers.ListStringTypeHandler;
import com.shengqin.config.handlers.TestDateRangeTypeHandler;
import com.shengqin.pojo.plan.TestDateRangeModel;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Data
@TableName(value = "t_plan_reservation_item", autoResultMap = true)
public class PlanReservationItem {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer planId;
    @TableField(value = "projects", typeHandler = ListStringTypeHandler.class)
    private List<String> projects;

    private Integer maxNum;
    private Boolean enabled;
    private String address;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    private Integer stepMaxNum;

    @TableField(value = "test_day_list", typeHandler = TestDateRangeTypeHandler.class)
    private List<TestDateRangeModel> testDayList;

    private String remark;


    @TableField(exist = false)
    private Map<String,List<TestDateRangeModel>> canSelectDateMap;

    @TableField(exist = false)
    private String startDay;
    @TableField(exist = false)
    private String endDay;

    public Map<String,List<TestDateRangeModel>> getCanSelectDateMap() {
        Map<String,List<TestDateRangeModel>> canSelectDates = new HashMap<>();
        if (!CollectionUtils.isEmpty(testDayList)) {
            AtomicReference<LocalDate> minDate = new AtomicReference<>(null);
            AtomicReference<LocalDate> maxDate = new AtomicReference<>(null);
            testDayList.forEach(testDateRangeModel -> {
                if (StringUtils.hasLength(testDateRangeModel.getStart()) && StringUtils.hasLength(testDateRangeModel.getEnd())) {
                    LocalDate startDate = LocalDate.parse(testDateRangeModel.getStart(), TestDateRangeTypeHandler.dateFormatter);
                    if (minDate.get() == null) {
                        minDate.set(startDate);
                        this.startDay = minDate.get().format(TestDateRangeTypeHandler.dateFormatter);
                    } else {
                        if (minDate.get().isAfter(startDate)) {
                            minDate.set(startDate);
                            this.startDay = minDate.get().format(TestDateRangeTypeHandler.dateFormatter);
                        }
                    }
                    LocalDate endDate = LocalDate.parse(testDateRangeModel.getEnd(), TestDateRangeTypeHandler.dateFormatter);
                    if (maxDate.get() == null) {
                        maxDate.set(endDate);
                        this.endDay = maxDate.get().format(TestDateRangeTypeHandler.dateFormatter);
                    } else {
                        if (maxDate.get().isBefore(endDate)) {
                            maxDate.set(endDate);
                            this.endDay = maxDate.get().format(TestDateRangeTypeHandler.dateFormatter);
                        }
                    }
                    while (startDate.isBefore(endDate)) {
                        canSelectDates.put(startDate.format(TestDateRangeTypeHandler.dateFormatter),testDateRangeModel.getTestTimeList());
                        startDate = startDate.plusDays(1);
                    }
                    canSelectDates.put(endDate.format(TestDateRangeTypeHandler.dateFormatter),testDateRangeModel.getTestTimeList());
                }
            });
        }
        return canSelectDates;
    }
}
